package com.atzon.controller;

import com.alibaba.fastjson.JSON;
import com.atzon.service.MeetService;
import com.atzon.service.impl.MeetServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/meet/*")
public class MeetServlet extends BaseServlet {
    private MeetService meetService = new MeetServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("currentPage");
            String search = request.getParameter("search");
            if (search != null) {
                if (search.equals("")) {
                    search = null;
                }
            }
            String dept = request.getParameter("dep");
            if (dept != null) {
                if (dept.equals("请选择部门")) {
                    dept = null;
                }
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            if (null == search && null == dept) {
                response.sendRedirect(request.getContextPath() + "/view/meeting?page=" + page);
            } else {
                if (null != search && null != dept) {
                    search = URLEncoder.encode(search, "utf-8");
                    dept = URLEncoder.encode(dept, "utf-8");
                    response.sendRedirect(request.getContextPath() + "/view/meeting?page=" + page + "&search=" + search + "&dep=" + dept);
                } else if (null != dept) {
                    dept = URLEncoder.encode(dept, "utf-8");
                    response.sendRedirect(request.getContextPath() + "/view/meeting?page=" + page + "&dep=" + dept);
                } else {
                    search = URLEncoder.encode(search, "utf-8");
                    response.sendRedirect(request.getContextPath() + "/view/meeting?page=" + page + "&search=" + search);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addMyMeet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String title = req.getParameter("title");
            String content = req.getParameter("content");
            String dept = req.getParameter("dep");
            String start = req.getParameter("start");
            Map<String, Object> userBaseInfo = (Map<String, Object>) req.getSession().getAttribute("userBaseInfo");
            Integer userId = (Integer) userBaseInfo.get("id");
            meetService.addMeet(dept, title, content, start, userId);
            resp.sendRedirect(req.getContextPath() + "/view/meetAdd");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delJoin(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map<String, Object> userBaseInfo = (Map<String, Object>) req.getSession().getAttribute("userBaseInfo");
            Integer userId = (Integer) userBaseInfo.get("id");
            String cId = req.getParameter("cId");
            meetService.delJoin(userId, Integer.parseInt(cId));
            String jsonData = JSON.toJSONString(null);
            resp.getWriter().write(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addJoin(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map<String, Object> userBaseInfo = (Map<String, Object>) req.getSession().getAttribute("userBaseInfo");
            Integer userId = (Integer) userBaseInfo.get("id");
            String cId = req.getParameter("cId");
            meetService.addJoin(userId, Integer.parseInt(cId));
            String jsonData = JSON.toJSONString(null);
            resp.getWriter().write(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void meetEnd(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String cId = req.getParameter("cId");
            Map<String, Object> meet = meetService.findMeetById(Integer.parseInt(cId));
            Time begTime = (Time) meet.get("begTime");
            Date pubDate = (Date) meet.get("pubDate");
            SimpleDateFormat sdfL = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfR = new SimpleDateFormat("HH:mm:ss");
            String meetTime = sdfL.format(pubDate) + " " + sdfR.format(begTime);
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date begmeet = sdf.parse(meetTime);
            Date now = new Date();
            if (now.getTime() - begmeet.getTime() > 0) {
                meetService.turnstu(0, Integer.parseInt(cId));
            }
            String jsonData = JSON.toJSONString(null);
            resp.getWriter().write(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void meetBeg(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String cId = req.getParameter("cId");
            meetService.turnstu(2, Integer.parseInt(cId));
            String jsonData = JSON.toJSONString(null);
            resp.getWriter().write(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
